package com.yixun.qfbao.hander;

import com.yixun.qfbao.api.service.PremisesApiService;
import com.yixun.qfbao.apidto.GetPremisesCityListReqDto;
import com.yixun.qfbao.apidto.GetPremisesCityListResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.vo.GetPremisesCityListReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author guifang.weng
 * @create 2017-12-01 14:07
 **/
@Service
public class GetPremisesCityListHander extends CommonAbstractHander {
    private static Logger logger= LoggerFactory.getLogger(GetPremisesCityListHander.class);

    @Autowired
    private PremisesApiService premisesApiService;
    @Autowired
    private LoginUtils loginUtils;

    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        BaseResponseDTO<GetPremisesCityListResDto> res = new BaseResponseDTO<>();
        GetPremisesCityListReqVo reqVo = (GetPremisesCityListReqVo)reqDto;
        GetPremisesCityListReqDto getPremisesCityListReqDto = reqVo.getParams();
        if(StringUtils.isNotBlank(getPremisesCityListReqDto.getUserId())){
        	   PartnerInfo pi = loginUtils.getUser(getPremisesCityListReqDto.getUserId());
               if (pi == null) {
                   res.setResult(ResultEnum.FAIL.getNo());
                   res.setErrorMsg("未找到用户信息");
                   return res;
               }
               getPremisesCityListReqDto.setMerchantId(pi.getHigherid());
        }
        try {
            GetPremisesCityListResDto resDto = premisesApiService.getPremisesCityListService(getPremisesCityListReqDto);
            res.setResult(resDto.getResult());
            res.setResData(resDto);
        } catch (Exception e) {
            logger.error("GetPremisesCityListHander error: {}", e);
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("获取城市信息失败");
        }
        return res;
    }
}
